#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>   //      gettimeofday
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>



#include "p2v.h"
#include "typedef.h"
#include "udp_get_bitrate.h"


extern void LCDDisplayASCII8X16(u32 x0, u32 y0, u32 ForeColor, char * s);
extern u32 g_pLCDBuffer;

uint16_t x_res = 1920;
uint16_t y_res = 1200;

int *switch_mode;
int pre_switch_mode;
int switch_mode_pre=0;

#define RED     (0xFFFF0000)
#define BLUE    (0xFF0000FF)
#define GREEN   (0xFF00FF00)
#define YELLOW  (0xFFFFFF00)
#define BLACK   (0xFF000000)


struct display_param{
          int freq;
          int dbm1;
          int dbm2;
          int bw;
          int power;
          int bitrate;
};

int *switch_memory_open(void) {
        char buf[128];
        int fd;
        sprintf(buf, "/switch_mode");
        fd = shm_open(buf, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
        if(fd < 0) { perror("shm_open"); exit(1); }
        if (ftruncate(fd, 4096) == -1) { perror("ftruncate"); exit(1); }
        int *retval = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        return retval;
}



void blt_clr(uint32_t *fb, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
        uint32_t *src_addr;
        int x_stride;
        int i;
        int iter = y1 - y0 + 1;

        x_stride = (x1 - x0 + 1) * 4;
        for(i=0; i<iter; i++) {
                src_addr = &fb[(y0 + i)*x_res + x0];
                memset(src_addr, 0x0, x_stride);
        }
}



#define BG_COLOR 0xff0000ff
void blt_bg(uint32_t *fb, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
        uint32_t *src_addr;
        int x_stride;
        int i;
        int j;
        int iter = y1 - y0 + 1;

        x_stride = (x1 - x0 + 1);
        for(i=0; i<iter; i++) {
                src_addr = &fb[(y0 + i)*x_res + x0];
                //memset(src_addr, 0x0, x_stride);
                for(j = 0; j < x_stride; j++)
                        src_addr[j] = BG_COLOR;
        }
}

void blt_bg_white(uint32_t *fb, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
        uint32_t *src_addr;
        int x_stride;
        int i;
        int j;
        int iter = y1 - y0 + 1;

        x_stride = (x1 - x0 + 1);
        for(i=0; i<iter; i++) {
                src_addr = &fb[(y0 + i)*x_res + x0];
                //memset(src_addr, 0x0, x_stride);
                for(j = 0; j < x_stride; j++)
                        src_addr[j] = 0xff000000;
        }
}




void update_power(uint32_t *fb, struct display_param *param,int draw)
  {
          char str_buf[128];
 	  int x,y;
          if(!draw)
                  return;
//          printf("ant1 power:%.2d ant2 power:%.2d \n",param->dbm1,param->dbm2);
          sprintf(str_buf, "ant1 power %.1ddbm ant2 power %.1ddbm", param->dbm1,param->dbm2);
          blt_bg(fb, 32, y_res-60+24, (x_res/2)-1, y_res-60+24+23);
          LCDDisplayASCII8X16(32, y_res-60+24, YELLOW, str_buf);
 
          blt_bg(fb, 64, 18, x_res, 18+24-1);
          sprintf(str_buf, "Freq %d.%dMHz BW %.2dMHz tx gain %.2ddbm", param->freq/10,param->freq%10, param->bw/1000, param->power);
          LCDDisplayASCII8X16(64, 18, YELLOW, str_buf);

	  if(pre_switch_mode == 6){
	  	//blt_clr(fb,100,100,100+)  
		x=100;
		y=100;
		blt_clr(fb,x,y,x+16*6,y+24);
		blt_clr(fb,x+1920/2,y,x+1920/2+16*6,y+24);
		blt_clr(fb,x+1920/2,y+1080/3,  x+1920/2+16*6,y+1080/3+24);
		blt_clr(fb,x+1920/2,y+1080*2/3,x+1920/2+16*6,y+1080*2/3+24);
		blt_clr(fb,x,y+1080/2,x+16*6,y+1080/2+24);
		blt_clr(fb,x+1920/2,y+1080/2,x+1920/2+16*6,y+1080/2+24);

		if(switch_mode_pre ==0){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
			LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
			LCDDisplayASCII8X16(x,        y+1080/2,  RED, "VIDEO3");
			LCDDisplayASCII8X16(x+1920/2, y+1080/2,  RED, "VIDEO4");
		}else if(switch_mode_pre==1){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
		}else if(switch_mode_pre==2){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO2");
		}else if(switch_mode_pre==3){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO3");
		
		}else if(switch_mode_pre==4){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO4");
		}else if(switch_mode_pre==5){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
			LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
			LCDDisplayASCII8X16(x+1920/2, y+1080/3,         RED, "VIDEO3");
			LCDDisplayASCII8X16(x+1920/2, y+1080*2/3,         RED, "VIDEO4");
		}else if(switch_mode_pre == 6){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
                        LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
                        LCDDisplayASCII8X16(x,        y+1080/2,  RED, "VIDEO3");
                        LCDDisplayASCII8X16(x+1920/2, y+1080/2,  RED, "VIDEO4");
		}
	  }else if(pre_switch_mode == 4){
		x=100;
		y=100;
		blt_clr(fb,x,y,x+16*6,y+24);
		blt_clr(fb,x+1920/2,y,x+1920/2+16*6,y+24);
		blt_clr(fb,x+1920/2,y+1080/2,x+1920/2+16*6,y+1080/2+24);		

		if(switch_mode_pre ==0){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
			LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
			LCDDisplayASCII8X16(x+1920/2, y+1080/2,  RED, "VIDEO3");
		}else if(switch_mode_pre ==1){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
		}else if(switch_mode_pre ==2){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO2");
		}else if(switch_mode_pre == 3){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO3");
		}else if(switch_mode_pre == 4){
			LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
                        LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
                        LCDDisplayASCII8X16(x+1920/2, y+1080/2,  RED, "VIDEO3");

		}

	  }else if(pre_switch_mode == 3){

		x=100;
		y=100;
		blt_clr(fb,x,y,x+16*6,y+24);
		blt_clr(fb,x+1920/2,y,x+1920/2+16*6,y+24);

		if(switch_mode_pre ==0){
                        LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
                        LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
                }else if(switch_mode_pre ==1){
                        LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
                }else if(switch_mode_pre ==2){
                        LCDDisplayASCII8X16(x,        y,         RED, "VIDEO2");
                }else if(switch_mode_pre == 3){
                        LCDDisplayASCII8X16(x,        y,         RED, "VIDEO1");
			LCDDisplayASCII8X16(x+1920/2, y,         RED, "VIDEO2");
                }

	  }
  }





int main(int argc, char *argv[])
{
	uint32_t *fb_base0;
	uint32_t *fb_base1;
	uint32_t *fb_ctrl;
	int i;
	int alpha;
 	int logo_fd;
	char disp_str[128];
	struct display_param param;
	int frame0;
	int fd_param;
	char *buffer_param;
	char freq[10];
	char dbm1[5];
	char dbm2[5];
	int flag_nosignal;




	if(argc != 3) {
		printf("memset alpha[int] switch mode\n");
		return -1;
	}

	pre_switch_mode = atoi(argv[2]);
	fb_base0 = (int32_t *)p2v(0x11800000, 1920*1200*4);
	printf("fb_base virt 0x%08x\n", (uint32_t)fb_base0);
	alpha = atoi(argv[1]);

	memset(fb_base0,0x00,1920*1200*4);
	fb_base1 = (int32_t *)p2v(0x12100000, 1920*1200*4);
        printf("fb_base virt 0x%08x\n", (uint32_t)fb_base1);
	
	memset(fb_base1,0x00,1920*1200*4);
	fb_ctrl = (int32_t *)p2v(0x43c00000, 4096);

	i = 0;


	blt_bg(fb_base0, 0, 0, x_res-1, 59);
        blt_bg(fb_base0, 0, 1140, x_res-1, y_res-1);

        blt_bg(fb_base1, 0, 0, x_res-1, 59);
        blt_bg(fb_base1, 0, 1140, x_res-1, y_res-1);



	udp_get_bitrate_init(8010);

	param.freq=14429;
	param.dbm1=-44;
	param.dbm2=-46;
	param.bw=10000;
	param.power = 25;
	param.bitrate = 8000;

	    fd_param = shm_open("/param.txt",O_RDWR|O_CREAT|O_EXCL,0777);
        if(fd_param < 0){
               fd_param = shm_open("/param.txt",O_RDWR,0777);
        }else
                ftruncate(fd_param,4096);
         buffer_param= mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd_param,0);


	

	switch_mode=switch_memory_open();


	while(1) {
		if(buffer_param[20] == 1){

			if(flag_nosignal == 1){
				memset(fb_base1,0x00,1920*1200*4);
				memset(fb_base0,0x00,1920*1200*4);
				blt_bg(fb_base0, 0, 0, x_res-1, 59);
        			blt_bg(fb_base0, 0, 1140, x_res-1, y_res-1);

        			blt_bg(fb_base1, 0, 0, x_res-1, 59);
        			blt_bg(fb_base1, 0, 1140, x_res-1, y_res-1);
			
			}
			flag_nosignal = 0;
			memcpy(freq,buffer_param,10);
			memcpy(dbm1,buffer_param+10,5);
			memcpy(dbm2,buffer_param+15,5);

		//	printf("++++%s %s %s\n",freq,dbm1,dbm2);
			//param.freq=24765;
			param.freq =14429;
			//param.freq=8157;
        		param.dbm1=atoi(dbm1);
        		param.dbm2=atoi(dbm2);
		//	param.bitrate = udp_get_bitrate();	

	
			fb_ctrl[56] = 0x5;
			g_pLCDBuffer = (u32)fb_base1;
			update_power(fb_base1,&param,frame0%60 == 0);		


			fb_ctrl[56] = 0x7;
			g_pLCDBuffer = (u32)fb_base0;
			update_power(fb_base0,&param,frame0%60 == 0);

			frame0++;
		}else{
			// no signal
			flag_nosignal = 1;
			fb_ctrl[56] = 0x5;
			g_pLCDBuffer = (u32)fb_base1;
			blt_bg_white(fb_base1,0,60,1920,1140);
			LCDDisplayASCII8X16(1000, 18, RED, "No Signal");
			LCDDisplayASCII8X16(32, y_res-60+24, YELLOW, "ant1 power    dbm  ant2 power    dbm");
			
			fb_ctrl[56] = 0x7;
			g_pLCDBuffer = (u32)fb_base0;
			blt_bg_white(fb_base0,0,60,1920,1140);
			LCDDisplayASCII8X16(32, y_res-60+24, YELLOW, "ant1 power    dbm  ant2 power    dbm");
			usleep(200000);						

		}
		if(switch_mode_pre != switch_mode[0]){
			printf("++++++++++++%d\n",switch_mode[0]);
			switch_mode_pre = switch_mode[0];
		}
		usleep(20000);
	}	

	return 0;
}
